//
//  MyView.m
//  yonghu
//
//  Created by 浪尖渝力 on 2018/1/15.
//  Copyright © 2018年 com.DJS. All rights reserved.
//

#import "MyView.h"
#import "UserInfoViewController.h"
#import "MyBillViewController.h"
#import "CollectionViewController.h"
#import "newsViewController.h"
#import "sheZhiViewController.h"
#import "fanKuiViewController.h"
#import "DownloadViewController.h"
#import "JiFenViewController.h"
@implementation MyView

-(void)creatView{
    self.backgroundColor=_LightGrey;
    
    _scrollView=[UIScrollView new];
    [self addSubview:_scrollView];
    [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
    _scrollView.scrollsToTop =NO;
    
    _scrollView.showsVerticalScrollIndicator = FALSE;
    _scrollView.showsHorizontalScrollIndicator = FALSE;
    _scrollView.bounces = NO;
    _scrollView.contentSize = CGSizeMake(0,650);
    
    UIImageView *beiJing=[UIImageView new];
    [_scrollView addSubview:beiJing];
    [beiJing mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(0);
        make.width.mas_equalTo(_mainW);
        make.height.mas_equalTo(180);
    }];
    beiJing.image=[UIImage imageNamed:@"个人中心背景"];
    
    [self creatTopView];
    
    UIView *centerView=[UIView new];
    [_scrollView addSubview:centerView];
    [centerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(5);
        make.width.mas_equalTo(_mainW-10);
        make.top.mas_equalTo(beiJing.mas_bottom).mas_equalTo(50);
        make.height.mas_equalTo(200);
    }];
    centerView.backgroundColor=[UIColor whiteColor];
    centerView.layer.masksToBounds=YES;
    centerView.layer.cornerRadius=5;
    [self creatCenterView:centerView];
    
    UIView *bottomView=[UIView new];
    [_scrollView addSubview:bottomView];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(5);
        make.width.mas_equalTo(_mainW-10);
        make.top.mas_equalTo(centerView.mas_bottom).mas_equalTo(10);
        make.height.mas_equalTo(150);
    }];
    bottomView.backgroundColor=[UIColor whiteColor];
    bottomView.layer.masksToBounds=YES;
    bottomView.layer.cornerRadius=5;
    [self creatBottomView:bottomView];
    
}
-(void)creatTopView{
    
    UIView *topView=[UIView new];
    [_scrollView addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.width.mas_equalTo(_mainW-40);
        make.top.mas_equalTo(100);
        make.height.mas_equalTo(120);
    }];
    topView.backgroundColor=[UIColor whiteColor];
    topView.layer.masksToBounds=YES;
    topView.layer.cornerRadius=5;
    
    UIView  *beiJingImage=[UIView new];
    [_scrollView addSubview:beiJingImage];
    [beiJingImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(topView);
        make.width.height.mas_equalTo(100);
        make.top.mas_equalTo(60);
    }];
    beiJingImage.backgroundColor=[UIColor whiteColor];
    beiJingImage.layer.masksToBounds=YES;
    beiJingImage.layer.cornerRadius=50;
    
    UIImageView *userIamge=[UIImageView new];
    [_scrollView addSubview:userIamge];
    [userIamge mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(beiJingImage);
        make.width.height.mas_equalTo(98);
    }];
    userIamge.layer.masksToBounds=YES;
    userIamge.layer.cornerRadius=49;
    NSString *wl_practice_photo=[_userDic objectForKey:@"user_phone"];

    
    userIamge.userInteractionEnabled=YES;
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pushToLogin:)];
    [userIamge addGestureRecognizer:tapGesture];
    
    UILabel *userLabel=[UILabel new];
    [topView addSubview:userLabel];
    [userLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(beiJingImage.mas_bottom).mas_equalTo(15);
    }];
    
    
    userLabel.textColor=ColorWithRGB(49, 49, 49);
    userLabel.font=[UIFont systemFontOfSize:20];
    userLabel.userInteractionEnabled=YES;
    UITapGestureRecognizer *tapGesture2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pushToLogin:)];
    [userLabel addGestureRecognizer:tapGesture2];
    if (_userDic==nil) {
        userLabel.text=@"登录/注册";
        userIamge.image=[UIImage imageNamed:@"未登录头像"];
    }
    else{
        
        NSString *news_cover=[_userDic objectForKey:@"user_icon"];
        if ([ZQTools charIsNil:news_cover]) {
             userIamge.image=[UIImage imageNamed:@"未登录头像"];
        }
        else{
            news_cover=[NSString stringWithFormat:@"%@%@",_addressUrl,news_cover];
            [userIamge sd_setImageWithURL:[NSURL URLWithString:news_cover]];
            userLabel.text=[_userDic objectForKey:@"user_nickname"];
        }
       
    }

}
-(void)creatCenterView:(UIView *)view{
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    
    NSString *isOrSH=[user objectForKey:@"isOrSH"];
    if (isOrSH.integerValue==0) {
        NSArray *nameArr=[NSArray arrayWithObjects:@"我的积分",@"我的下载",@"我的账单",@"我的收藏", nil];
        for (int i=0; i<4; i++) {
            UIView *beiJingView=[UIView new];
            [view addSubview:beiJingView];
            [beiJingView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.mas_equalTo(0);
                make.height.mas_equalTo(50);
                make.top.mas_equalTo(50*i);
            }];
            beiJingView.tag=i;
            UITapGestureRecognizer *tapGesture2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pushToDingDan:)];
            [beiJingView addGestureRecognizer:tapGesture2];
            
            UIImageView *imageView=[UIImageView new];
            [beiJingView addSubview:imageView];
            [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(10);
                make.centerY.mas_equalTo(0);
                make.width.height.mas_equalTo(25);
            }];
            imageView.image=[UIImage imageNamed:nameArr[i]];
            
            UILabel *label=[UILabel new];
            [beiJingView addSubview:label];
            [label mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(imageView.mas_right).mas_equalTo(15);
                make.centerY.mas_equalTo(0);
            }];
            label.text=nameArr[i];
            label.textColor=ColorWithRGB(49, 49, 49);
            label.font=[UIFont systemFontOfSize:16];
            
            UIImageView *jianTou=[UIImageView new];
            [beiJingView addSubview:jianTou];
            [jianTou mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.mas_equalTo(-10);
                make.centerY.mas_equalTo(0);
                make.width.mas_equalTo(10);
                make.height.mas_equalTo(15);
            }];
            jianTou.image=[UIImage imageNamed:@"iconfont-fanhui-拷贝-3"];
            
            if (i==0 && _userDic!=nil) {
                UILabel *label2=[UILabel new];
                [beiJingView addSubview:label2];
                [label2 mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerY.mas_equalTo(0);
                    make.right.mas_equalTo(jianTou.mas_left).mas_equalTo(-5);
                }];
                label2.text=[NSString stringWithFormat:@"%@",[_userDic objectForKey:@"user_integral"]];
                label2.textColor=ColorWithRGB(149, 149, 149);
                label2.font=[UIFont systemFontOfSize:15];
                
                
            }
        }
    
    }
    else{
        NSArray *nameArr=[NSArray arrayWithObjects:@"我的积分",@"我的下载",@"我的收藏", nil];
        for (int i=0; i<3; i++) {
            UIView *beiJingView=[UIView new];
            [view addSubview:beiJingView];
            [beiJingView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.mas_equalTo(0);
                make.height.mas_equalTo(50);
                make.top.mas_equalTo(50*i);
            }];
            beiJingView.tag=i;
            UITapGestureRecognizer *tapGesture2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pushToDingDan:)];
            [beiJingView addGestureRecognizer:tapGesture2];
            
            UIImageView *imageView=[UIImageView new];
            [beiJingView addSubview:imageView];
            [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(10);
                make.centerY.mas_equalTo(0);
                make.width.height.mas_equalTo(25);
            }];
            imageView.image=[UIImage imageNamed:nameArr[i]];
            
            UILabel *label=[UILabel new];
            [beiJingView addSubview:label];
            [label mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(imageView.mas_right).mas_equalTo(15);
                make.centerY.mas_equalTo(0);
            }];
            label.text=nameArr[i];
            label.textColor=ColorWithRGB(49, 49, 49);
            label.font=[UIFont systemFontOfSize:16];
            
            UIImageView *jianTou=[UIImageView new];
            [beiJingView addSubview:jianTou];
            [jianTou mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.mas_equalTo(-10);
                make.centerY.mas_equalTo(0);
                make.width.mas_equalTo(10);
                make.height.mas_equalTo(15);
            }];
            jianTou.image=[UIImage imageNamed:@"iconfont-fanhui-拷贝-3"];
            
            if (i==0 && _userDic!=nil) {
                UILabel *label2=[UILabel new];
                [beiJingView addSubview:label2];
                [label2 mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerY.mas_equalTo(0);
                    make.right.mas_equalTo(jianTou.mas_left).mas_equalTo(-5);
                }];
                label2.text=[NSString stringWithFormat:@"%@",[_userDic objectForKey:@"user_integral"]];
                label2.textColor=ColorWithRGB(149, 149, 149);
                label2.font=[UIFont systemFontOfSize:15];
                
                
            }
        }
    }
    
}
-(void)creatBottomView:(UIView *)view{
    NSArray *nameArr=[NSArray arrayWithObjects:@"消息",@"设置",@"意见反馈", nil];
    for (int i=0; i<3; i++) {
        UIView *beiJingView=[UIView new];
        [view addSubview:beiJingView];
        [beiJingView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(0);
            make.height.mas_equalTo(50);
            make.top.mas_equalTo(50*i);
        }];
        beiJingView.tag=i;
        UITapGestureRecognizer *tapGesture2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pushToXiTong:)];
        [beiJingView addGestureRecognizer:tapGesture2];
        
        UIImageView *imageView=[UIImageView new];
        [beiJingView addSubview:imageView];
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(10);
            make.centerY.mas_equalTo(0);
            make.width.height.mas_equalTo(25);
        }];
        imageView.image=[UIImage imageNamed:nameArr[i]];
        
        UILabel *label=[UILabel new];
        [beiJingView addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(imageView.mas_right).mas_equalTo(15);
            make.centerY.mas_equalTo(0);
        }];
        label.text=nameArr[i];
        label.textColor=ColorWithRGB(49, 49, 49);
        label.font=[UIFont systemFontOfSize:16];
        
        UIImageView *jianTou=[UIImageView new];
        [beiJingView addSubview:jianTou];
        [jianTou mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-10);
            make.centerY.mas_equalTo(0);
            make.width.mas_equalTo(10);
            make.height.mas_equalTo(15);
        }];
        jianTou.image=[UIImage imageNamed:@"iconfont-fanhui-拷贝-3"];
    }
    
}

-(void)pushToLogin:(UITapGestureRecognizer *)tap{
    if (_userDic.count==0) {
        [ZQTools ToLoging:self.viewController];
    }
    else{
        UserInfoViewController *userInfo=[[UserInfoViewController alloc] init];
        userInfo.dic=_userDic;
        [ZQTools pushNextViewController:self.viewController  andRootController:userInfo];
        
    }
    
}

-(void)pushToDingDan:(UITapGestureRecognizer *)tap{
    
    NSDictionary *userDic = [NSKeyedUnarchiver unarchiveObjectWithFile:_userModelFile];
    if (userDic==nil) {
        [ZQTools ToLoging:self.viewController];
        return;
    }
    if (tap.view.tag==0) {
        JiFenViewController *geRen=[[JiFenViewController alloc] init];
        [ZQTools pushNextViewController:self.viewController andRootController:geRen];
    }
    if (tap.view.tag==1) {
        DownloadViewController *geRen=[[DownloadViewController alloc] init];
        [ZQTools pushNextViewController:self.viewController andRootController:geRen];
    }
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    
    NSString *isOrSH=[user objectForKey:@"isOrSH"];
    if (isOrSH.integerValue==0) {
        if (tap.view.tag==2) {
            
            MyBillViewController *geRen=[[MyBillViewController alloc] init];
            [ZQTools pushNextViewController:self.viewController andRootController:geRen];
        }  if (tap.view.tag==3) {
            CollectionViewController *geRen=[[CollectionViewController alloc] init];
            [ZQTools pushNextViewController:self.viewController andRootController:geRen];
            
        }
    }
    else{
        if (tap.view.tag==2) {
            CollectionViewController *geRen=[[CollectionViewController alloc] init];
            [ZQTools pushNextViewController:self.viewController andRootController:geRen];
        }
    }
    
}

-(void)pushToXiTong:(UITapGestureRecognizer *)tap{
    
    NSDictionary *userDic = [NSKeyedUnarchiver unarchiveObjectWithFile:_userModelFile];
    if (userDic==nil) {
        [ZQTools ToLoging:self.viewController];
        return;
    }

    if (tap.view.tag==0) {
        NewsViewController *geRen=[[NewsViewController alloc] init];
        [ZQTools pushNextViewController:self.viewController andRootController:geRen];
        
    }  if (tap.view.tag==1) {
        sheZhiViewController *geRen=[[sheZhiViewController alloc] init];
        [ZQTools pushNextViewController:self.viewController andRootController:geRen];
        
    }if (tap.view.tag==2) {
        fanKuiViewController *geRen=[[fanKuiViewController alloc] init];
        [ZQTools pushNextViewController:self.viewController andRootController:geRen];
        
    }
    
}



@end
